﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Transfer;

namespace BranchOfSansar.Register.Trans
{
    public partial class frmTransRun : BranchOfSansar.Register.Trans.frmTransBase
    {
        public frmTransRun()
        {
            InitializeComponent();
        }

        public frmTransRun(long contract_no):this()
        {
            SetContractNo(contract_no);            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            save();
        }

        private void frmTransRun_Load(object sender, EventArgs e)
        {

        }

        private void save()
        {
            if (Contract == null) return;
            List<Transaction> txn = new List<Transaction>();
            List<SendTransfer> strans = new List<SendTransfer>();
            //BranchOfSansar.Sansar.Enquiry.Product p = (BranchOfSansar.Sansar.Enquiry.Product)txtProdCode.Properties.GetDataSourceRowByDisplayValue(txtProdCode.Text);

            if (Contract.Status != 2)
            {
                Globals.Warr("Түр хаасан гэрээ биш байна!");
                return;
            }
            //Contract.p
            DateTime pausedDate = Contract.PauseDate ?? Globals.GetDate();
            double pausedDays = (Globals.GetDate() - pausedDate).TotalDays;
            DateTime now=Globals.GetDate();
            SendContract con = new SendContract()
            {
                ContractNo = Contract.ContractNo,
                //Balance = txtLastBalance.Value,
                EndDate = (Contract.EndDate ?? Globals.GetDate()).AddDays(pausedDays),
                //StartDate = Contract.StartDate ?? txtDate.DateTime,
                Status = 0 //Хэвийн болгох  
            };

            txn.Add(new Transaction()
            {
                ContractNo = Contract.ContractNo,
                ProdCode = 100001,
                Quantity = 1,
                StartDate = null,
                EndDate = null,
                Status = 1,
                TxnCategory = "RUN",
                BankNo = null,
                TxnPrice = 0,
                TxnAmount = 0,
                TxnDebtAmount = 0,
                TxnFirstBalance = decimal.Parse(txtBalance.Text),
                TxnDate = DateTime.Now,
                TxnDesc = txtDesc.Text.Length > 0 ? txtDesc.Text : "Түр хаалтыг нээв",
                TxnType = 1001,
                UserNo = Globals.UserId
            });


            if (Contract.ProdType == "F") //Урдчилан төлбөрт бол?
            {
                using (TransferClient tcc = new TransferClient(Globals.Token))
                {
                    vw_Transaction[] btrans = tcc.TransactionList(string.Format("it.ContractNo={0} AND it.Status=4", Contract.ContractNo));

                    for (int t=0; t < btrans.Length;t++ )
                    {
                        strans.Add(new SendTransfer()
                        {
                            TransNo = btrans[t].TransNo,
                            //EndDate = Globals.SystemDate,
                            StartDate=(btrans[t].StartDate ?? pausedDate).AddDays(pausedDays),
                            EndDate = (btrans[t].EndDate ?? pausedDate).AddDays(pausedDays),
                            
                            //TxnDate=now,
                            Status=1
                            //Status = 4
                        });
                        
                    }
                }
            }
            else //Дараа төлбөрт
            {
                con.BalanceDate = Globals.SystemDate;
            }

            using (TransferClient tc = new TransferClient(Globals.Token))
            {
                var res = tc.DoTransaction(txn.ToArray(), strans.ToArray(), con);
                if (res.Code != 0)
                    Globals.Warr(res.Message);
                else
                {
                    Globals.Info(res.Message);
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
            }
            btnSave.Enabled = false;
        }
        private void frmTransRun_ContractSelected()
        {
            pnlTransfer.Enabled = true;
            //Хэрэв урьдчилсан төлбөрт бол
            btnSave.Enabled = true;
        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
    }
}
